package servlets;

import beans.Promotion;
import dao.DAOFactory;
import dao.PromotionDAO;
import forms.AjouterPromotionForm;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(name = "CreerPromotion", urlPatterns = {"/creerPromotion"})
public class CreerPromotion extends HttpServlet {

   public static final String CONF_DAO_FACTORY = "daofactory";
   public static final String ATT_EXAM         = "promotion";
   public static final String ATT_FORM         = "form";
   public static final String VUE              = "/WEB-INF/creerPromotion.jsp";
    
   private PromotionDAO promotionDAO;
   
    @Override
   public void init() throws ServletException {
        /* Récupération d'une instance de notre DAO Matiere */
        this.promotionDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getPromotionDao();
   }
   
      @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        /* Affichage de la page */
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        /* Préparation de l'objet formulaire */
        AjouterPromotionForm form = new AjouterPromotionForm( promotionDAO );
 
        /* Traitement de la requête et récupération du bean en résultant */
        Promotion promotion = form.creerPromotion( request );
         
        /* Stockage dans l'objet request */
        request.setAttribute( ATT_FORM, form );
        request.setAttribute( ATT_EXAM, promotion );
 
        /* Affichage de la page */
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
        
    }

}
